import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../function';
import {
  getStockHistoryOrder,
  getOptionHistoryOrder,
  getStockTradeLogListByStock,
  getOptionTradeLogListByStock
} from '../../../services/history_order_service';

export default {
  namespace: 'history',
  state: {
    stocks: { code: 0, message: '', list: [] },
    stocksFilters: {},
    options: { code: 0, message: '', list: [] },
    optionsFilters: {},
  },

  effects: {
    // stock
    *GetStockTradeLogListByStock({ payload, callback }, { call, put }) {
      const response = yield call(getStockTradeLogListByStock, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
    // options
    *GetOptionTradeLogListByStock({ payload, callback }, { call, put }) {
      const response = yield call(getOptionTradeLogListByStock, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // stocks
    *GetStockHistoryOrder({ payload, callback }, { call, put }) {
      const response = yield call(getStockHistoryOrder, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveStocks',
          payload: {
            list: { code: 1, message: response.message, list: response.list },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },

    // options
    *GetOptionHistoryOrder({ payload, callback }, { call, put }) {
      const response = yield call(getOptionHistoryOrder, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response !== 'undefined' && response.code === 1) {
        yield put({
          type: 'saveOptions',
          payload: {
            list: { code: 1, message: response.message, list: response.list },
            filter: {},
          },
        });
        if (callback) callback(response);
      } else {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      }
    },
  },

  reducers: {
    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.list,
        stocksFilters: action.payload.filter,
      };
    },

    saveOptions(state, action) {
      return {
        ...state,
        options: action.payload.list,
        optionsFilters: action.payload.filter,
      };
    },
  },
};
/* import { getStockHistoryOrder,
  getOptionHistoryOrder,
} from '../../../services/history_order_service'
import { notification, message } from 'antd';
import { GetDateThree, FormatnumberZero } from '../../../function'
import cloneDeep from 'lodash/cloneDeep'
import _map from 'lodash/map'
import _uniq from 'lodash/uniq'
import _pull from 'lodash/pull'

export default {
  namespace: 'history',
  state: {
    stocks: {code: 0, message: "",list:[]},
    stocksFilters: {},
    options: {code: 0, message: "",list:[]},
    optionsFilters: {},
  },

  effects: {

//stocks
    * GetStockHistoryOrder({payload,callback}, {call, put}) {
      let response = yield call(getStockHistoryOrder, payload);
      if(typeof(response)==='undefined'){return;}
      if(typeof(response)!=='undefined'&&response.code===1) {
        yield put({
          type: 'saveStocks',
          payload: {list:{code: 1, message:response.message, list:response.list,},filter:{}},
        });
        if (callback) callback(response);
      }else{
        notification.error({
          message:typeof response.message !=='undefined'?response.message:'请求错误'
        });
        return;
      }
    },


//options
    * GetOptionHistoryOrder({payload,callback}, {call, put}) {
      let response = yield call(getOptionHistoryOrder, payload);if(typeof(response)==='undefined'){return;}

      if(typeof(response)!=='undefined'&&response.code===1) {
        yield put({
          type: 'saveOptions',
          payload: {list:{code: 1, message:response.message, list:response.list,},filter:{}},
        });
        if (callback) callback(response);
      }else{
        notification.error({
          message:typeof response.message !=='undefined'?response.message:'请求错误'
        });
        return;
      }
    },

  },

  reducers: {
    saveStocks(state, action) {
      return {
        ...state,
        stocks: action.payload.list,
        stocksFilters: action.payload.filter,
      };
    },

    saveOptions(state, action) {
      return {
        ...state,
        options: action.payload.list,
        optionsFilters: action.payload.filter,
      };
    },

  }

};
*/
